﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Taobao.Common;
using Taobao.Tools2.Domain;

namespace Taobao.Tools2
{
    public class Logger:ILogger
    {
        #region Ctor
        public Logger(IRepository<Log> logRepository)
        {
            LogRepository = logRepository;
        }
        #endregion

        #region Ctor

        protected IRepository<Log> LogRepository { get; set; }
        #endregion

        #region Implement ILogger
        public void Write(string module,LogType type,string message,string traceTrack, IShop shop)
        {
            message = message.Length > 500 ? message.Substring(0, 500) : message;
            traceTrack = traceTrack.Length > 1900 ? traceTrack.Substring(0, 1900) : traceTrack;
            Taobao.Tools2.Domain.Log log = new Domain.Log();
            log.Message = message;
            log.Module = module;
            log.TraceStack = traceTrack;
            log.Type = type.ToString();
            log.ShopID = shop.ID;
            log.Created = DateTime.Now;
            LogRepository.Add(log); 
        }

        public void WriteLogToFile(string fileName, string logContent)
        {
            using (StreamWriter sWriter = new StreamWriter(fileName,false))
            {
                sWriter.Write(logContent);
                sWriter.Close();
            }
        }
        #endregion
         
    }
}
